<template>
  <div style="padding: 0 15px;">
    <div class="box">
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <h3 style="line-height: 32px">
              <i class="el-icon-s-tools" /> &nbsp; 管理信息
            </h3>
          </div>
        </el-col>
        <el-col :span="16" style="text-align: right">
          <div class="grid-content bg-purple">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click="addAttrDialogVisible = true"
            >添加属性模板</el-button>
            <el-button
              size="small"
              type="primary"
              icon="el-icon-remove-outline"
            >删除属性模板</el-button>
          </div>
        </el-col>
      </el-row>
      <div class="divider" />

      <el-row :gutter="15" class="whererow">
        <el-col :span="4" style="line-height: 32px">当前共4个模板。</el-col>
        <el-col :span="6">
          <el-input
            v-model="search"
            type="text"
            size="small"
            placeholder="请输入模板名称"
          >
            <i slot="prefix" class="el-input__icon el-icon-search" />
          </el-input>
        </el-col>
      </el-row>
    </div>

    <div class="noticerow box">
      <i class="el-icon-edit" /> &nbsp; 已选择了0项
    </div>

    <el-table
      ref="multipleTable"
      border
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="编号" width="60" type="index" />
      <el-table-column align="center" label="模板名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <!-- <el-table-column align="center" label="属性数量" width="100">
                <template slot-scope="scope">{{ scope.row.attrCount }}</template>
            </el-table-column>
            <el-table-column align="center" label="参数数量" width="100">
                <template slot-scope="scope">{{ scope.row.paramCount }}</template>
            </el-table-column> -->
      <el-table-column align="center" label="设置" width="300">
        <template slot-scope="scope">
          <el-button
            title="非销售属性列表"
            size="mini"
            icon="el-icon-tickets"
            @click="
              $router.push(
                `/home/product/nosalesattributemanage/${scope.row.id}`
              )
            "
          >非销售属性列表</el-button>
          <el-button
            title="销售属性列表"
            size="mini"
            icon="el-icon-document-copy"
            @click="
              $router.push(
                `/home/product/salesattributemanage/${scope.row.id}`
              )
            "
          >销售属性列表</el-button>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="170">
        <template slot-scope="scope" type="index">
          <el-button
            title="编辑属性模板"
            type="warning"
            size="mini"
            icon="el-icon-edit"
            circle
            @click="showUpdateTemplateDialog(scope.$index)"
          />
          <el-button
            title="删除属性模板"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            circle
            @click="deleteAttrTemplate(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        :current-page="currentPage"
        layout="total, prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog
      title="添加属性模版"
      :visible.sync="addAttrDialogVisible"
      :modal-append-to-body="false"
    >
      <el-form :model="addForm">
        <el-form-item label="请选择分类" prop="categoryId" label-width="120px">
          <el-cascader
            ref="cascader"
            v-model="addForm.categoryId"
            filterable
            placeholder="试试 搜索关键字"
            :props="props"
          />
        </el-form-item>
        <el-form-item label="属性模版名称" prop="name" label-width="120px">
          <el-input v-model="addForm.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="属性模版拼音" prop="pinyin" label-width="120px">
          <el-input v-model="addForm.pinyin" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="属性模版关键词"
          prop="keywords"
          label-width="120px"
        >
          <el-input v-model="addForm.keywords" autocomplete="off" />
        </el-form-item>
        <el-form-item label="属性模版序号" prop="sort" label-width="120px">
          <el-input v-model="addForm.sort" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addAttrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddAttrForm()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="修改属性模版"
      :visible.sync="updateAttrDialogVisible"
      :modal-append-to-body="false"
    >
      <el-form :model="updateForm">
        <el-form-item label="请选择分类" prop="categoryId" label-width="120px">
          <el-cascader
            ref="cascader"
            v-model="updateForm.categoryId"
            filterable
            placeholder="点击修改默认类别"
            :props="props"
          />
        </el-form-item>
        <el-form-item label="属性模版名称" prop="name" label-width="120px">
          <el-input v-model="updateForm.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="属性模版拼音" prop="pinyin" label-width="120px">
          <el-input v-model="updateForm.pinyin" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="属性模版关键词"
          prop="keywords"
          label-width="120px"
        >
          <el-input v-model="updateForm.keywords" autocomplete="off" />
        </el-form-item>
        <el-form-item label="属性模版序号" prop="sort" label-width="120px">
          <el-input v-model="updateForm.sort" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateAttrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateAttrForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addAttrDialogVisible: false,
      updateAttrDialogVisible: false,
      selectedTemplateId: 0, // 当前选中模板ID
      addForm: {},
      updateForm: {},
      currentPage: 1, // 当前页
      total: 0, // 总页数
      pageSize: 5, // 每页条目数
      search: '',
      tableData: [],
      props: {
        lazy: true, // 懒加载
        emitPath: false, // 设置 false，只返回该节点的值
        lazyLoad: (node, resolve) => {
          /** 定义懒加载方式 */
          console.log('选择的当前节点：', node)
          let parentId = 0
          if (!node.root) {
            parentId = node.data.value
          }
          this.categoryApi
            .queryByParent({ parentId: parentId })
            .then((res) => {
              console.log('加载子类别：', res)
              if (res.data.state === 200) {
                // 整理数据结构, 返回类别数组 [{label:'类别名', value:'类别id'}...]
                const nodes = []
                res.data.data.forEach((item) => {
                  nodes.push({
                    label: item.name,
                    value: item.id,
                    leaf: item.parent !== 1
                  })
                })
                resolve(nodes)
              } else {
                this.$alert(res.data.message, '提示')
              }
            })
        }
      }
    }
  },

  mounted() {
    // 加载所有属性模板列表
    this.loadAttrtempList()
  },
  methods: {
    /** 显示修改模板弹出窗口 */
    showUpdateTemplateDialog(index) {
      console.log(index)
      // 将选中项的模板ID，存入data.selectedTemplateId 供提交表单所用
      this.updateForm = this.tableData[index]
      this.updateAttrDialogVisible = true
    },

    /** 删除所选中的属性模板 */
    deleteAttrTemplate(id) {
      // 弹出确认对话框
      this.$confirm('确认删除此属性模板？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.attrTemplateApi.delete({ id }).then((res) => {
          console.log('删除选中的属性模板：', res)
          if (res.data.state === 200) {
            this.$message({
              showClose: true,
              message: '删除属性模板成功',
              type: 'success'
            })
            // 刷新属性模板列表
            this.loadAttrtempList()
          } else {
            this.$alert(res.data.message, '提示')
          }
        })
      })
    },

    /** 提交添加属性模版的表单 */
    submitAddAttrForm() {
      this.attrTemplateApi.add(this.addForm).then((res) => {
        console.log('添加属性模板：', res)
        if (res.data.state === 200) {
          // 添加成功，给出提示，让弹窗消失
          this.$message({
            showClose: true,
            message: '恭喜你，新增属性模板成功',
            type: 'success'
          })
          this.addAttrDialogVisible = false
          // 刷新属性模板列表
          this.loadAttrtempList()
        }
      })
    },

    /** 提交修改属性模版的表单 */
    submitUpdateAttrForm() {
      this.attrTemplateApi.update(this.updateForm).then((res) => {
        console.log('修改属性模板：', res)
        if (res.data.state === 200) {
          // 添加成功，给出提示，让弹窗消失
          this.$message({
            showClose: true,
            message: '恭喜你，修改属性模板成功',
            type: 'success'
          })
          this.updateAttrDialogVisible = false
          // 刷新属性模板列表
          this.loadAttrtempList()
        } else {
          this.$alert(res.data.message, '提示')
        }
      })
    },

    /** 加载所有属性模板列表 */
    loadAttrtempList() {
      // const page = this.currentPage
      // const pageSize = this.pageSize
      // this.attrTemplateApi.queryAll({ page, pageSize }).then((res) => {
      //   console.log('加载所有属性模板对象:', res)
      //   this.tableData = res.data.data.list
      //   this.total = res.data.data.total
      // })
    },

    /** 处理更新页码后的业务 */
    handleCurrentChange(val) {
      console.log(`即将加载选中页: 第${val}页`)
      this.currentPage = val
      this.loadAttrtempList()
    }
  }
}
</script>

